<?php
        
        include '../../auth/ceklogin.php';
        include '../../sql/conn.php';
        
        $username=$_SESSION['userbkknt'];
        $no_po_in=$_REQUEST['no_po_in'];
        $idCustomer=$_REQUEST['idCustomer'];
        $desc_po_in=$_REQUEST['desc_po_in'];
        $dpp_po_in=$_REQUEST['dpp_po_in'];
        
        //ambil lokasi user
        $query_ambillokasiuser=mysql_query("select lokasiUser from user where uname='$username'");
        $row_ambillokasiuser=mysql_fetch_array($query_ambillokasiuser);
        $lokasiUser=$row_ambillokasiuser[0];
        if($lokasiUser=='surabaya'){
            $lokasiUser="SBY";
        }elseif($lokasiUser=='jakarta'){
            $lokasiUser="JKT";
        }
        
        //ambil nomor job terakhir
        $query_finish_1=mysql_query("
            select no_job 
            from po_in 
            where no_job not like 'temp%' 
            order by no_job desc 
            limit 0,1
            ");
        $row_finish_1=mysql_fetch_array($query_finish_1);
        $tmp=explode("/",$row_finish_1[0]);

        //tahun
        $tahun_tmp=$tmp[4];
        $tahun_tmp=explode('-',$tahun_tmp);
        $tahun_tmp=$tahun_tmp[1];

        //nomer job
        $no_job=$tahun_tmp[0];
        
        //cek tahun sekarang apakah sudah ganti tahun
        if($tahun_tmp==date("y")){
                $new=$no_job+1;
        }
        else{
                $new=1;
        }

        //nomor baru
        $new=sprintf("%03d",$new);
//        $len=strlen($new);
//        for($len;$len<4;$len++){
//        $new="0".$new;
//        }

        $no="POT/S/".$lokasiUser."/".$new."-".date("y");
        

        $query_po_in_finish=mysql_query("
            INSERT INTO po_in
            VALUE(null,'$no_po_in','$no','$idCustomer','$desc_po_in','$dpp_po_in','0','$username',now())
        ");
        
        if($query_po_in_finish){
            echo json_encode(array('success'=>true));
        } else{
            echo json_encode(array('msg'=>'Query Error.'.  mysql_error()));
        }
?>